package com.zsinfo.guoss.supplier.activity;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.netcenter.Event.Response;
import com.example.netcenter.RetrofitBuilder;
import com.example.netcenter.Url.ConstantsCode;
import com.example.netcenter.bean.BankListBean;
import com.jcodecraeer.xrecyclerview.ProgressStyle;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.MultiItemTypeAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;
import com.zsinfo.guoss.supplier.R;
import com.zsinfo.guoss.supplier.base.BaseActivity;
import com.zsinfo.guoss.supplier.utils.SharedPreferencesUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by lixl on 2018/12/5.
 * <p>
 * desc:退款账户列表
 */
public class RefundAccountListActivity extends BaseActivity implements View.OnClickListener {

    private LinearLayout ll_ali_account;
    private TextView tv_type;
    private TextView tv_no;

    private XRecyclerView xRecyclerView;
    private CommonAdapter myAdapter;
    private List<BankListBean.DataBean.ObjectsBean> accountData = new ArrayList<>();
    private int pageNo = 1;
    private int pageSize = 20;
    private boolean isLast = false; //是否是最后一条数据
    private boolean isRefresh = false, isLoadMore = false; //默认不刷新，不加载

    @Override
    protected int getContentLayoutRes() {
        return R.layout.activity_refund_account;
    }

    @Override
    protected void initView() {
        setTitle("选择退款账户");
        setBackAndLeftTitle("").setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        ll_ali_account = findViewById(R.id.ll_ali_account);
        tv_type = findViewById(R.id.tv_my_cash_type);
        tv_no = findViewById(R.id.bank_card_no);

        //显示支付宝账号信息
        ll_ali_account.setOnClickListener(this);
        tv_type.setText("支付宝");
        tv_no.setText(SharedPreferencesUtil.getALiCount());

        initAdapter();
    }

    @Override
    protected void initNetData() {
        GetNetCenter().GetNetCenter(this, RetrofitBuilder.CreateApi().BANK_CARD_LIST(ConstantsCode.bank_card_list, SharedPreferencesUtil.getID(), pageSize+"", pageNo+""), new Response() {
            @Override
            public void OK(Object data) {
                setDataView(data);
            }

            @Override
            public void Fail(Throwable e) {

            }

            @Override
            public void complete() {
                if (isRefresh) {
                    xRecyclerView.refreshComplete();
                }
                if (isLoadMore) {
                    xRecyclerView.loadMoreComplete();
                }
            }
        });
    }

    @Override
    protected void customDestroy() {

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.ll_ali_account:
                Intent intent = new Intent(RefundAccountListActivity.this, RefundActivity.class);
                intent.putExtra("bankCardId", "");  //支付宝没有id，传空就可以了
                intent.putExtra("bankCardName", "支付宝");
                intent.putExtra("bankCardNo", SharedPreferencesUtil.getALiCount());
                setResult(4, intent);
                finish();
                break;
        }
    }

    private void initAdapter() {
        xRecyclerView = findViewById(R.id.recyclerview);
        xRecyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        xRecyclerView.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader);
        xRecyclerView.setLoadingMoreProgressStyle(ProgressStyle.BallRotate);
        xRecyclerView.setArrowImageView(R.mipmap.iconfont_downgrey);
        myAdapter = new CommonAdapter<BankListBean.DataBean.ObjectsBean>(this, R.layout.item_refund_account, accountData) {
            @Override
            protected void convert(final ViewHolder holder, BankListBean.DataBean.ObjectsBean bean, int position) {
                String endNum = "";
                //Display the card of bank last 4 num
                if (bean.getCard().length() > 4) {
                    endNum = bean.getCard().substring(bean.getCard().length() - 4, bean.getCard().length());
                } else {
                    endNum = bean.getCard();
                }
                holder.setText(R.id.tv_my_cash_type, bean.getBank());
                holder.setText(R.id.bank_card_no, "尾号:" + endNum);
            }
        };
        xRecyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                //刷新操作
                xRecyclerView.loadMoreComplete();
                //清除数据
                accountData.clear();
                //更新适配器
                myAdapter.notifyDataSetChanged();
                //刷新操作
                pageNo = 1;
                //重新请求接口
                initNetData();
                //正在执行刷新
                isRefresh = true;
            }

            @Override
            public void onLoadMore() {
                //加载更多
                //可以判断 isLast=true，表示已经没有更多数据了
                if (isLast) {
                    showToast("当前已经没有更多数据了");
                    xRecyclerView.loadMoreComplete(); //停止加载的动画效果
                    return;
                }
                //加载更多
                pageNo++;
                //请求加载接口
                initNetData();
                //正在执行加载
                isLoadMore = true;
            }
        });
        myAdapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
                //选中银行卡，将数据给传递过去
                position --;
                Intent intent = new Intent(RefundAccountListActivity.this, RefundActivity.class);
                intent.putExtra("bankCardId", accountData.get(position).getId());
                intent.putExtra("bankCardName", accountData.get(position).getBank());
                String card = accountData.get(position).getCard();  //只保留卡号后4位
                if (card.length() > 4) {
                    card = card.substring(card.length() - 4, card.length());
                }
                intent.putExtra("bankCardNo", card);
                setResult(4, intent);
                finish();
            }

            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
                return false;
            }
        });
        xRecyclerView.setAdapter(myAdapter);
    }

    /**
     * 设置数据到view
     *
     * @param data
     */
    private void setDataView(Object data) {
        BankListBean bankListBean = (BankListBean) data;
        //赋值
        isLast = bankListBean.getData().isIsLast();
        //判断一下，currentPageNo==1是表示第一次加载数据，如果第一次加载数据为空，就表示没有数据
        if (pageNo == 1) {
            accountData.clear();
            accountData.addAll(bankListBean.getData().getObjects());
        } else {
            accountData.addAll(bankListBean.getData().getObjects());
        }
        myAdapter.notifyDataSetChanged();
    }

}
